/****************************************************************************
**
** This file is part of qPhotoTag, a photo keywording application
**
** Copyright (C) 2009 Marcell Lengyel <miketkf@gmail.com>
**
** GNU General Public License
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**
****************************************************************************/

#include "centraleurroman.h"

QT_BEGIN_NAMESPACE

static quint16 const values[128] =
{	0x00C4, 0x0100, 0x0101, 0x00C9, 0x0104, 0x00D6, 0x00DC, 0x00E1,
	0x0105, 0x010C, 0x00E4, 0x010D, 0x0106, 0x0107, 0x00E9, 0x0179,
	0x017A, 0x010E, 0x00ED, 0x010F, 0x0112, 0x0113, 0x0116, 0x00F3,
	0x0117, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x011A, 0x011B, 0x00FC,
	0x2020, 0x00B0, 0x0118, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF,
	0x00AE, 0x00A9, 0x2122, 0x0119, 0x00A8, 0x2260, 0x0123, 0x012E,
	0x012F, 0x012A, 0x2264, 0x2265, 0x012B, 0x0136, 0x2202, 0x2211,
	0x0142, 0x013B, 0x013C, 0x013D, 0x013E, 0x0139, 0x013A, 0x0145,
	0x0146, 0x0143, 0x00AC, 0x221A, 0x0144, 0x0147, 0x2206, 0x00AB,
	0x00BB, 0x2026, 0x00A0, 0x0148, 0x0150, 0x00D5, 0x0151, 0x014C,
	0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA,
	0x014D, 0x0154, 0x0155, 0x0158, 0x2039, 0x203A, 0x0159, 0x0156,
	0x0157, 0x0160, 0x201A, 0x201E, 0x0161, 0x015A, 0x015B, 0x00C1,
	0x0164, 0x0165, 0x00CD, 0x017D, 0x017E, 0x016A, 0x00D3, 0x00D4,
	0x016B, 0x016E, 0x00DA, 0x016F, 0x0170, 0x0171, 0x0172, 0x0173,
	0x00DD, 0x00FD, 0x0137, 0x017B, 0x0141, 0x017C, 0x0122, 0x02C7
};

QCentralEurRomanCodec::QCentralEurRomanCodec() {
    reverseMap = new QByteArray();
    int m = 0;
    int i = 0;
    while(i < 128) {
        if (values[i] > m && values[i] < 0xfffd)
            m = values[i];
        i++;
    }
    m++;
    reverseMap->resize(m);
    for(i = 0; i < 128 && i < m; i++)
        (*reverseMap)[i] = (char)i;
    for(;i < m; i++)
        (*reverseMap)[i] = 0;
    for(i=128; i<256; i++) {
        int u = values[i-128];
        if (u < m)
            (*reverseMap)[u] = (char)(unsigned char)(i);
    }
}

QCentralEurRomanCodec::~QCentralEurRomanCodec() {
	delete reverseMap;
}


QByteArray QCentralEurRomanCodec::name() const {
	return "Apple Central European";
}

QList<QByteArray> QCentralEurRomanCodec::aliases() const {
  QList<QByteArray> list;
  list << "CentralEurRoman" << "maccenteurroman";
  return list;
}

int QCentralEurRomanCodec::mibEnum() const {
  return 2999;	/* not IANA certified? */
}


QString QCentralEurRomanCodec::convertToUnicode(const char* chars, int len, ConverterState *) const
{
    if (len <= 0 || chars == 0)
        return QString();

    const unsigned char * c = (const unsigned char *)chars;

    QString r;
    r.resize(len);
    QChar* uc = r.data();

    for (int i = 0; i < len; i++) {
        if (c[i] > 127)
            uc[i] = values[c[i]-128];
        else
            uc[i] = QLatin1Char(c[i]);
    }
    return r;
}

QByteArray QCentralEurRomanCodec::convertFromUnicode(const QChar *in, int length, ConverterState *state) const
{
    const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
    int invalid = 0;

    QByteArray r;
    r.resize(length);
    int i = length;
    int u;
    const QChar* ucp = in;
    unsigned char* rp = (unsigned char *)r.data();
    const unsigned char* rmp = (const unsigned char *)reverseMap->data();
    int rmsize = (int) reverseMap->size();    
    while(i--)
    {
        u = ucp->unicode();
        if (u < 128) {
            *rp = (char)u;
        } else {
            *rp = ((u < rmsize) ? (*(rmp+u)) : 0);
            if (*rp == 0) {
                *rp = replacement;
                ++invalid;
            }
        }
        rp++;
        ucp++;
    }

    if (state) {
        state->invalidChars += invalid;
    }
    return r;
}


QT_END_NAMESPACE

